package sk.baka.aedict.util;

import java.util.Formatter;

import sk.baka.aedict.AedictApp;
import sk.baka.aedict.R;
import sk.baka.aedict.dict.DictEntry;
import sk.baka.aedict.dict.EdictEntry;
import sk.baka.aedict.dict.TanakaDictEntry;
import android.app.Activity;
import android.content.Context;
import android.text.ClipboardManager;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;

public class CopyEntryClickListener implements OnMenuItemClickListener {

	Activity activity;
	DictEntry entry;
	boolean copyWholeEntry;
	private static final String ENTRY_FORMAT = "%s 【%s】 %s";
	private static final String ENTRY_FORMAT_NO_KANJI = "%s %s";
	
	public CopyEntryClickListener (Activity activity, DictEntry entry, boolean copyWholeEntry) {
		this.activity = activity;
		this.entry = entry;
		this.copyWholeEntry = copyWholeEntry;
	}
	private String getTextToCopy() {
		if (copyWholeEntry) {
			if (entry instanceof DictEntry) {
				final DictEntry e = (DictEntry) entry;
				if (e.kanji != null)  {
					return new Formatter().format(ENTRY_FORMAT, e.kanji, e.reading, e.english).toString();	
				} else {
					return new Formatter().format(ENTRY_FORMAT_NO_KANJI, e.reading, e.english).toString();
				}
			} else {
				return entry.getJapanese();
			}
		} else {
			return entry.getJapanese();
		}
	}
	public boolean onMenuItemClick(MenuItem item) {
		final ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
		String text = getTextToCopy();
		cm.setText(text);
		final Toast toast = Toast.makeText(activity, AedictApp.format(R.string.copied, text), Toast.LENGTH_SHORT);
		toast.show();
		return true;
	}

}
